#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
详细调试脚本 - 分析1688页面结构
"""

import time
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
from urllib.parse import quote

def debug_1688_page():
    """调试1688页面结构"""
    
    # 设置浏览器选项（使用Chromium）
    chrome_options = Options()
    
    # 指定Chromium二进制文件路径
    chrome_options.binary_location = '/usr/bin/chromium'
    
    chrome_options.add_argument('--no-sandbox')
    chrome_options.add_argument('--disable-dev-shm-usage')
    chrome_options.add_argument('--disable-blink-features=AutomationControlled')
    chrome_options.add_experimental_option("excludeSwitches", ["enable-automation"])
    chrome_options.add_experimental_option('useAutomationExtension', False)
    chrome_options.add_argument('--user-agent=Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36')
    
    # 指定ChromeDriver路径
    from selenium.webdriver.chrome.service import Service
    service = Service('/usr/bin/chromedriver')
    driver = webdriver.Chrome(service=service, options=chrome_options)
    driver.execute_script("Object.defineProperty(navigator, 'webdriver', {get: () => undefined})")
    
    try:
        # 测试不同的URL格式
        urls_to_test = [
            f"https://s.1688.com/selloffer/offer_search.htm?keywords={quote('数据线')}&beginPage=1&sortType=va_rmdesc&tab=offer",
            f"https://s.1688.com/selloffer/offer_search.htm?keywords={quote('数据线')}&beginPage=1",
            f"https://search.1688.com/service/marketOfferResultViewService?keywords={quote('数据线')}",
            f"https://s.1688.com/youyuan/index.htm?keywords={quote('数据线')}",
            "https://www.1688.com/"
        ]
        
        for i, url in enumerate(urls_to_test, 1):
            print(f"\n=== 测试URL {i}: {url} ===")
            
            try:
                driver.get(url)
                time.sleep(5)  # 等待页面加载
                
                # 检查页面标题
                title = driver.title
                print(f"页面标题: {title}")
                
                # 检查是否是登录页面
                login_indicators = [
                    "登录", "login", "passport", "账号", "密码"
                ]
                page_source = driver.page_source.lower()
                is_login_page = any(indicator in page_source for indicator in login_indicators)
                print(f"是否为登录页面: {is_login_page}")
                
                # 检查页面内容关键词
                content_keywords = ["数据线", "offer", "price", "商品", "产品"]
                found_keywords = [kw for kw in content_keywords if kw in page_source]
                print(f"找到的关键词: {found_keywords}")
                
                # 尝试查找商品相关元素
                selectors_to_check = [
                    '.offer-item',
                    '.sm-offer-item',
                    '.card-item',
                    '.offer-wrapper',
                    '[data-widget-cid*="offer"]',
                    '.list-item',
                    '[class*="offer"]',
                    '[class*="item"]',
                    '[class*="product"]',
                    '[class*="goods"]'
                ]
                
                found_elements = {}
                for selector in selectors_to_check:
                    try:
                        elements = driver.find_elements(By.CSS_SELECTOR, selector)
                        if elements:
                            found_elements[selector] = len(elements)
                    except Exception as e:
                        continue
                
                print(f"找到的元素: {found_elements}")
                
                # 如果找到了商品元素，尝试提取信息
                if found_elements:
                    best_selector = max(found_elements.items(), key=lambda x: x[1])
                    print(f"\n使用最佳选择器: {best_selector[0]} (找到{best_selector[1]}个元素)")
                    
                    elements = driver.find_elements(By.CSS_SELECTOR, best_selector[0])
                    for j, element in enumerate(elements[:3]):  # 只检查前3个
                        print(f"\n--- 元素 {j+1} ---")
                        try:
                            # 尝试提取文本内容
                            text = element.text.strip()[:100]  # 限制长度
                            print(f"文本内容: {text}")
                            
                            # 尝试找链接
                            links = element.find_elements(By.CSS_SELECTOR, "a[href]")
                            if links:
                                print(f"链接数量: {len(links)}")
                                print(f"第一个链接: {links[0].get_attribute('href')[:100]}")
                            
                            # 尝试找图片
                            images = element.find_elements(By.CSS_SELECTOR, "img")
                            if images:
                                print(f"图片数量: {len(images)}")
                                img_src = images[0].get_attribute('src') or images[0].get_attribute('data-src')
                                if img_src:
                                    print(f"第一个图片: {img_src[:100]}")
                                    
                        except Exception as e:
                            print(f"提取元素信息失败: {e}")
                
                # 保存页面源码（如果不是登录页面）
                if not is_login_page and found_keywords:
                    filename = f"debug_page_{i}.html"
                    with open(filename, 'w', encoding='utf-8') as f:
                        f.write(driver.page_source)
                    print(f"页面源码已保存到: {filename}")
                    
            except Exception as e:
                print(f"访问URL失败: {e}")
                
    finally:
        driver.quit()

if __name__ == "__main__":
    debug_1688_page()